package biz.almas.uycnr.crawler.crawler;

import java.io.IOException;
import java.util.List;

import biz.almas.uycnr.crawler.crawler.handler.ChannelXmlHandler;
import biz.almas.uycnr.crawler.crawler.handler.DocumentXmlHandler;
import biz.almas.uycnr.crawler.entity.Appendix;
import biz.almas.uycnr.crawler.entity.Channel;
import biz.almas.uycnr.crawler.entity.CnrDocument;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

public class UYCNRParser {

	/*
	 * public static List<Channel> parseChannel(String xmlPath) { List<Channel>
	 * list = new ArrayList<Channel>(); try {
	 * 
	 * // 读取文件内容 SAXReader reader = new SAXReader();
	 * 
	 * Document document = reader.read(xmlPath);
	 * 
	 * Element root = document.getRootElement();
	 * 
	 * Iterator<Element> iterator = root.elementIterator();
	 * 
	 * Channel mChannel = null; Element element = null; while
	 * (iterator.hasNext()) { element = iterator.next(); mChannel = new
	 * Channel(); mChannel.setIsContainsChildren(Integer.parseInt(element
	 * .elementText("ISCONTAINSCHILDREN")));
	 * mChannel.setChnlId(Integer.parseInt(element .elementText("chnlid")));
	 * mChannel.setChnlName(element.elementText("chnlname"));
	 * mChannel.setChnlDesc(element.elementText("chnldesc"));
	 * mChannel.setChnlType(Integer.parseInt(element .elementText("CHNLTYPE")));
	 * mChannel.setChnlOrder(Integer.parseInt(element
	 * .elementText("CHNLORDER"))); mChannel.setStatus(Integer.parseInt(element
	 * .elementText("status"))); mChannel.setParentId(Integer.parseInt(element
	 * .elementText("Parentid")));
	 * mChannel.setOperTime(element.elementText("OPERTIME"));
	 * mChannel.setCrTime(element.elementText("CRTIME"));
	 * mChannel.setDocumentsUrl(element.elementText("documents-url"));
	 * list.add(mChannel); } return list; } catch (Exception e) {
	 * e.printStackTrace(); }
	 * 
	 * return list; }
	 * 
	 * public static List<biz.almas.uycnr.crawler.entity.Document>
	 * parseDocument( String xmlPath) {
	 * List<biz.almas.uycnr.crawler.entity.Document> list = new
	 * ArrayList<biz.almas.uycnr.crawler.entity.Document>(); try { // 读取文件内容 //
	 * String file = CMyFile.readFile(xmlPath,"utf-8");
	 * 
	 * SAXReader reader = new SAXReader();
	 * 
	 * Document document = reader.read(xmlPath);
	 * 
	 * Element root = document.getRootElement();
	 * 
	 * Iterator<Element> iterator = root.elementIterator();
	 * 
	 * biz.almas.uycnr.crawler.entity.Document mDocument = null; Element element
	 * = null; while (iterator.hasNext()) { element = iterator.next(); mDocument
	 * = new biz.almas.uycnr.crawler.entity.Document();
	 * 
	 * mDocument.setDOCID(Integer.parseInt(element .elementText("DOCID")));
	 * mDocument.setDOCTITLE(element.elementText("DOCTITLE"));
	 * mDocument.setCRUSER(element.elementText("CRUSER"));
	 * mDocument.setDOCKEYWORDS(element.elementText("DOCKEYWORDS"));
	 * mDocument.setDOCSOURCE(element.elementText("DOCSOURCE"));
	 * mDocument.setDOCPUBTIME(element.elementText("DOCPUBTIME"));
	 * mDocument.setDOCRELTIME(element.elementText("DOCRELTIME"));
	 * mDocument.setCRTIME(element.elementText("CRTIME"));
	 * mDocument.setDOCHTMLCON(element.elementText("DOCHTMLCON"));
	 * mDocument.setOPERUSER(element.elementText("OPERUSER"));
	 * mDocument.setOPERTIME(element.elementText("OPERTIME"));
	 * mDocument.setDOCCONTENT(element.elementText("DOCCONTENT")); mDocument
	 * .setDOCPUBHTMLCON(element.elementText("DOCPUBHTMLCON"));
	 * mDocument.setDOCEDITOR(element.elementText("DOCEDITOR"));
	 * mDocument.setDOCAUTHOR(element.elementText("DOCAUTHOR"));
	 * mDocument.setDOCFIRSTPUBTIME(element .elementText("DOCFIRSTPUBTIME"));
	 * mDocument.setDOCOUTUPID(Integer.parseInt(element
	 * .elementText("DOCOUTUPID")));
	 * mDocument.setDOCCHANNEL(Integer.parseInt(element
	 * .elementText("DOCCHANNEL")));
	 * mDocument.setATTACHPIC(Integer.parseInt(element
	 * .elementText("ATTACHPIC")));
	 * mDocument.setDOCSTATUS(Integer.parseInt(element
	 * .elementText("DOCSTATUS")));
	 * mDocument.setDOCWORDSCOUNT(Integer.parseInt(element
	 * .elementText("DOCWORDSCOUNT")));
	 * mDocument.setDOCKIND(Integer.parseInt(element .elementText("DOCKIND")));
	 * mDocument.setSITEID(Integer.parseInt(element .elementText("SITEID")));
	 * mDocument.setDOCTYPE(Integer.parseInt(element .elementText("DOCTYPE")));
	 * 
	 * if (mDocument.getATTACHPIC() > 0) { try {
	 * 
	 * List<Element> files = element.element("FILES") .elements("FILE");
	 * Appendix appendix = null; for (Element file : files) { appendix = new
	 * Appendix(); appendix.setAPPAUTHOR(file.elementText("APPAUTHOR"));
	 * appendix.setAPPDESC(file.elementText("APPDESC"));
	 * appendix.setAPPENDIXID(Integer.parseInt(file
	 * .elementText("APPENDIXID")));
	 * appendix.setAPPFILE(file.elementText("APPFILE"));
	 * appendix.setAPPFILETYPE(Integer.parseInt(file
	 * .elementText("APPFILETYPE"))); appendix.setAPPLINKALT(file
	 * .elementText("APPLINKALT")); appendix.setAPPSERN(Integer.parseInt(file
	 * .elementText("APPSERN")));
	 * appendix.setAPPTIME(file.elementText("APPTIME"));
	 * appendix.setCRTIME(file.elementText("CRTIME"));
	 * appendix.setFILEEXT(file.elementText("FILEEXT"));
	 * appendix.setFILEURL(file.elementText("FILEURL")); }
	 * mDocument.setmAppendix(appendix); } catch (Exception e) {
	 * mDocument.setmAppendix(null); }
	 * 
	 * }
	 * 
	 * list.add(mDocument); } return list; } catch (Exception e) {
	 * e.printStackTrace(); }
	 * 
	 * return list; }
	 */
	
	//定义日志输出对象
    public static Logger logger = Logger.getLogger(UYCNRParser.class);
    
	private SAXParserFactory factory = SAXParserFactory.newInstance();

	private SAXParser parser;

	private List<CnrDocument> documents;

	private List<Appendix> appendixs;
	
	private List<Channel> channels;

	public boolean parseDocument(String xmlPath, int langId) {
		try {
			parser = factory.newSAXParser();
			
			DocumentXmlHandler handler = new DocumentXmlHandler();
			handler.langId = langId;
			
			parser.parse(xmlPath, handler);

			documents = handler.getDocuments();

			appendixs = handler.getAppendixs();

			return true;
		} catch (ParserConfigurationException e) {
			logger.error("解析文档是出错：" + e.toString());
			return false;
		} catch (SAXException e) {
			logger.error("解析文档是出错：" + e.toString());
			return false;
		} catch (IOException e) {
			logger.error("解析文档是出错：" + e.toString());
			return false;
		}
	}

	public boolean parseChannel(String xmlPath, int langId) {
		try {
			parser = factory.newSAXParser();
			
			ChannelXmlHandler handler = new ChannelXmlHandler();
			handler.langId = langId;
			
			parser.parse(xmlPath, handler);
			
			channels = handler.getChannels();
			
			return true;
		} catch (ParserConfigurationException e) {
			logger.error("解析栏目是出错：" + e.toString());
			return false;
		} catch (SAXException e) {
			logger.error("解析栏目是出错：" + e.toString());
			return false;
		} catch (IOException e) {
			logger.error("解析栏目是出错：" + e.toString());
			return false;
		}
		
	}

	public List<CnrDocument> getDocuments() {
		return documents;
	}

	public List<Appendix> getAppendixs() {
		return appendixs;
	}

	public List<Channel> getChannels() {
		return channels;
	}

}
